import { defineStore } from 'pinia';
import { ref } from 'vue';
import type { User } from '@/types/user';
import { getStorage, removeStorage, saveStorage } from '@/utils/storage';

export const useUserStore = defineStore('cp-user', () => {
  const user = ref<User>(JSON.parse(getStorage('users')) || ({} as User));

  // 存储用户信息（登录）
  const setUser = (userData: User) => {
    user.value = userData;
    saveStorage('users', JSON.stringify(userData));
  };

  // 删除用户信息（退出）
  const delUser = () => {
    user.value = {} as User;
    removeStorage('users');
  };

  // 返回变量和方法
  return {
    user,
    setUser,
    delUser,
  };
});
